package mysql

import "gorm.io/gorm"

type Department struct {
	gorm.Model
	HospitalId     int    `gorm:"type:int(10);not null;comment:'医院Id'"`
	FId            int    `gorm:"type:int(10);not null;comment:'父Id'"`
	DepartmentName string `gorm:"type:varchar(50);not null;comment:'科室名称'"`
}

func DepartmentList(pages, size int, DepartmentName string) (result []*Department, err2 error) {
	page := pages
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	tx := DB.Debug()
	if DepartmentName != "" {
		tx = tx.Where("department_name Like ?", "%"+DepartmentName+"%")
	}
	err2 = tx.Offset(offset).Limit(pageSize).Find(&result).Error
	if err2 != nil {
		return nil, err2
	}
	return
}
func DepartmentShow(id int) (result *Department, err2 error) {
	DB.Debug().Where("id = ?", id).Limit(1).Find(&result)
	return
}
